package com.lovo.csc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.csc.dto.PrepayDto;
import com.lovo.csc.entity.Deposit;
import com.lovo.csc.entity.Discount;
import com.lovo.csc.entity.Prepay;
import com.lovo.csc.mapper.DepositMapper;
import com.lovo.csc.service.IDepositService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.csc.service.IDiscountService;
import com.lovo.csc.service.IPrepayService;
import com.lovo.remoteservice.IPayService;
import com.lovo.sscweb.dto.MoneyDto;
import com.lovo.util.MQUtil;
import com.lovo.vo.TopUpVO;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.net.InetAddress;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Enumeration;
import java.util.List;

/**
 * <p>
 * 每次存款的记录 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@Service
public class DepositServiceImpl extends ServiceImpl<DepositMapper, Deposit> implements IDepositService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    IPrepayService prepayService;
    @Autowired
    IDiscountService discountService;
    @Autowired
    IDepositService service;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    IPayService payService;
    @RabbitListener(queues = MQUtil.moneyQueue)
    @Transactional
    public void orderDead(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, TopUpVO moneyDto) throws IOException {
            if (moneyDto == null){
                return;
            }
                String userCode = moneyDto.getUserCode();
                double money = moneyDto.getCurrentMoney();
//                Deposit deposit=new Deposit();
                // 得到预存款表的编号
                QueryWrapper<Prepay> qw= new QueryWrapper<>();
                qw.eq("user_code",userCode);
                Prepay prepay=prepayService.getOne(qw);
                // 如果用户不存在   则创建会员用户
                if (prepay == null){
                    prepay=new Prepay();
                    prepay.setUserCode(userCode);
                    prepay.setBalance(money);
                }else {
                    prepay.setBalance(money);
                }
//                float discountRate = 0;
                 PrepayDto prepayDto =new PrepayDto();
                // 这里循环一次用户充值后的金额是否达到新会员的标准
                List<Discount> discountList = discountService.list();
                for (Discount discount: discountList){
                    if (discount.getMinRequire()<=prepay.getBalance()){
                        prepay.setUserGrade(discount.getMembership());
                        prepayDto.setDiscountRate(discount.getDiscountRate());
                    }
                }

                //添加余额或新建用户，保存
                prepayService.saveOrUpdate(prepay);
//                //充值记录的设置
//                deposit.setPrepayId(prepay.getPrepayId());
//                deposit.setUserCode(userCode);
//                deposit.setSaveMoney(money);
//                deposit.setSaveWay("现金支付");
//                DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//                deposit.setSaveTime(df.format(LocalDateTime.now()));

                // 这里充值成功，通知前端

                prepayDto.setBalance(prepayDto.getBalance());
                prepayDto.setUserCode(prepay.getUserCode());
                prepayDto.setUserGrade(prepay.getUserGrade());

                // 新增折扣率
//                QueryWrapper<Discount> discountQueryWrapper=new QueryWrapper<>();
//                discountQueryWrapper.eq("membership",prepayDto.getUserGrade());
//                Discount discount = discountService.getOne(discountQueryWrapper);
//                prepayDto.setDiscountRate(discount.getDiscountRate());
                rabbitTemplate.convertAndSend(MQUtil.PrepayExchange,MQUtil.depositKey, prepayDto);
//                service.save(deposit);
                channel.basicAck(tag,false);
            }

}
